home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / bash_completion.d / mount < prev    next >
Encoding:
Text File  |  2010-11-16  |  4.7 KB  |  166 lines

  1. # mount(8) completion. This will pull a list of possible mounts out of
  2. # /etc/{,v}fstab, unless the word being completed contains a ':', which
  3. # would indicate the specification of an NFS server. In that case, we
  4. # query the server for a list of all available exports and complete on
  5. # that instead.
  6. #
  7. have mount &&
  8. {
  9.  
  10. # Just like COMPREPLY=(`compgen -W "${COMPREPLY[*]}" -- "$cur"`), only better!
  11. #
  12. # This will correctly escape special characters in COMPREPLY.
  13. _reply_compgen_array()
  14. {
  15.     # Create the argument for compgen -W by escaping twice.
  16.     #
  17.     # One round of escape is because we want to reply with escaped arguments. A
  18.     # second round is required because compgen -W will helpfully expand it's
  19.     # argument.
  20.     local i wlist
  21.     for i in ${!COMPREPLY[*]}; do
  22.         local q=$(quote "$(printf %q "${COMPREPLY[$i]}")")
  23.         wlist+=$q$'\n'
  24.     done
  25.  
  26.     # We also have to add another round of escaping to $cur.
  27.     local ecur="$cur"
  28.     ecur="${ecur//\\/\\\\}"
  29.     ecur="${ecur//\'/\'}"
  30.  
  31.     # Actually generate completions.
  32.     local oldifs=$IFS
  33.     IFS=$'\n' eval 'COMPREPLY=(`compgen -W "$wlist" -- "${ecur}"`)'
  34.     IFS=$oldifs
  35. }
  36.  
  37. # Unescape strings in the linux fstab(5) format (with octal escapes).
  38. __linux_fstab_unescape() {
  39.     eval $1="'${!1//\'/\047}'"
  40.     eval $1="'${!1/%\\/\\\\}'"
  41.     eval "$1=$'${!1}'"
  42. }
  43.  
  44. # Complete linux fstab entries.
  45. #
  46. # Reads a file from stdin in the linux fstab(5) format; as used by /etc/fstab
  47. # and /proc/mounts.
  48. _linux_fstab()
  49. {
  50.     COMPREPLY=()
  51.  
  52.     # Read and unescape values into COMPREPLY
  53.     local fs_spec fs_file fs_other
  54.     local oldifs="$IFS"
  55.     while read -r fs_spec fs_file fs_other; do
  56.         if [[ $fs_spec = [#]* ]]; then continue; fi
  57.         if [[ $1 == -L ]]; then
  58.             local fs_label=${fs_spec/#LABEL=}
  59.             if [[ $fs_label != "$fs_spec" ]]; then
  60.                 __linux_fstab_unescape fs_label
  61.                 IFS=$'\0'
  62.                 COMPREPLY+=("$fs_label")
  63.                 IFS=$oldifs
  64.             fi
  65.         else
  66.             __linux_fstab_unescape fs_spec
  67.             __linux_fstab_unescape fs_file
  68.             IFS=$'\0'
  69.             [[ $fs_spec = */* ]] && COMPREPLY+=("$fs_spec")
  70.             [[ $fs_file = */* ]] && COMPREPLY+=("$fs_file")
  71.             IFS=$oldifs
  72.         fi
  73.     done
  74.  
  75.     _reply_compgen_array
  76. }
  77.  
  78. _mount()
  79. {
  80.     local cur sm host prev
  81.  
  82.     COMPREPLY=()
  83.     _get_comp_words_by_ref -n : cur prev
  84.  
  85.     case $prev in
  86.         -t|--types)
  87.             _fstypes
  88.             return 0
  89.             ;;
  90.     esac
  91.  
  92.     [[ "$cur" == \\ ]] && cur="/"
  93.  
  94.     if [[ "$cur" == *:* ]]; then
  95.         for sm in "$(type -P showmount)" {,/usr}/{,s}bin/showmount; do
  96.             [ -x "$sm" ] || continue
  97.             COMPREPLY=( $( compgen -W "$( "$sm" -e ${cur%%:*} | \
  98.                 awk 'NR>1 {print $1}' )" -- "${cur#*:}" ) )
  99.             return 0
  100.         done
  101.     fi
  102.  
  103.     if [[ "$cur" == //* ]]; then
  104.         host=${cur#//}
  105.         host=${host%%/*}
  106.         if [ -n "$host" ]; then
  107.             COMPREPLY=( $( compgen -P "//$host" -W \
  108.                 "$( smbclient -d 0 -NL $host 2>/dev/null |
  109.                 sed -ne '/^['"$'\t '"']*Sharename/,/^$/p' |
  110.                 sed -ne '3,$s|^[^A-Za-z]*\([^'"$'\t '"']*\).*$|/\1|p' )" \
  111.                     -- "${cur#//$host}" ) )
  112.         fi
  113.     elif [ -r /etc/vfstab ]; then
  114.         # Solaris
  115.         COMPREPLY=( $( compgen -W "$( awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' /etc/vfstab )" -- "$cur" ) )
  116.     elif [ ! -e /etc/fstab ]; then
  117.         # probably Cygwin
  118.         COMPREPLY=( $( compgen -W "$( mount | awk '! /^[ \t]*#/ {if ($3 ~ /\//) print $3}' )" -- "$cur" ) )
  119.     else
  120.         # probably Linux
  121.         if [ "$prev" = -L ]; then
  122.             _linux_fstab -L < /etc/fstab
  123.         elif [ "$prev" = -U ]; then
  124.             COMPREPLY=( $( compgen -W '$(sed -ne "s/^[[:space:]]*UUID=\([^[:space:]]*\).*/\1/p" /etc/fstab )' -- "$cur" ) )
  125.         else
  126.             _linux_fstab < /etc/fstab
  127.         fi
  128.     fi
  129.  
  130.     return 0
  131. } &&
  132. complete -F _mount -o default -o dirnames mount
  133.  
  134. # umount(8) completion. This relies on the mount point being the third
  135. # space-delimited field in the output of mount(8)
  136. #
  137. have umount &&
  138. _umount()
  139. {
  140.     local cur
  141.     _get_comp_words_by_ref cur
  142.     COMPREPLY=()
  143.  
  144.     if [[ $(uname -s) = Linux && -r /proc/mounts ]]; then
  145.         # Linux /proc/mounts is properly quoted. This is important when
  146.         # unmounting usb devices with pretty names.
  147.         _linux_fstab < /proc/mounts
  148.     else
  149.         local IFS=$'\n'
  150.         COMPREPLY=( $( compgen -W '$( mount | cut -d" " -f 3 )' -- "$cur" ) )
  151.     fi
  152.  
  153.     return 0
  154. } &&
  155. complete -F _umount -o dirnames umount
  156.  
  157. }
  158.  
  159. # Local variables:
  160. # mode: shell-script
  161. # sh-basic-offset: 4
  162. # sh-indent-comment: t
  163. # indent-tabs-mode: nil
  164. # End:
  165. # ex: ts=4 sw=4 et filetype=sh
  166.